package com.yuanjin.util;

import java.util.Vector;

import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.yuanjin.R;

public class MyAdaper extends BaseAdapter {
	private Vector<FriendsItem> list = new Vector<FriendsItem>();
	Context context = null;
	private LayoutInflater inflater;

	public MyAdaper(Vector<FriendsItem> list, Context context) {
		this.list = list;
		this.context = context;
		inflater = LayoutInflater.from(context);

	}

	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder vh = null;
		if (convertView == null) {
			// 利用convertView实现空间的回收
			convertView = inflater.inflate(R.layout.friendslistitems, null);
			vh = new ViewHolder();
			vh.iv = (ImageView) convertView.findViewById(R.id.friendImageview);
			vh.tv = (TextView) convertView.findViewById(R.id.friendsTextview);
			// convertView 与vh建立对应关系
			convertView.setTag(vh);
		} else {
			vh = (ViewHolder) convertView.getTag();
		}
		// vh.iv.setBackgroundResource(R.drawable.yuanjin);
		//Log.e("myadapter", "xixixixiixixii");
		vh.tv.setText(list.get(position).name);
		return convertView;
	}

	public static class FriendsItem {
		public ImageView picture;
		public String name;
	}

	// 优化convertView检索image和name TextView
	class ViewHolder {
		ImageView iv;
		TextView tv;
	}

}
